TailleEditWin = {
	edited:false,
	isEditing:false,
	fieldViews:[],
	fields:[],
	titles:[],
	lines:[]
};

function findValue(tab, value) {
	for (var i in tab) {
		if (tab[i].title == value) {
			return i;
		}
	}
}

function updateNetPrice() {
	var qty = TailleEditWin.fields['Quantité'].value;
	var price = TailleEditWin.fields['Prix de vente'].text;
	var discount = TailleEditWin.fields['Remise (%)'].value;
	TailleEditWin.fields['Prix net'].text = price * (100 - discount) / 100;
}

function getRemisePickerData() {
	var data = [];
	for (var i = 0; i <= 100; i += 5) {
		data.push({title:'' + i});
	}
	return data;
}

function createEditableField(data, type) {
	var bt = Titanium.UI.createButton({
		style:Titanium.UI.iPhone.SystemButton.DISCLOSURE,
		width:30,
		height:30,
		top:0
	});
	
	var tf = Ti.UI.createTextField({
		value:data,
		keyboardType:Ti.UI.KEYBOARD_NUMBER_PAD,
		enabled:type == 'tf',
		paddingLeft:styles.editLeft,
		paddingRight:styles.editButtonPaddingRight,
		rightButton:bt,
		rightButtonMode:Ti.UI.INPUT_BUTTONMODE_ALWAYS,
		height:50,
		width:'100%'
	});

	if (type == 'tf') {
		bt.addEventListener('click', function(e) {
			tf.focus();
		});
		tf.addEventListener('focus', function(e) {
			TailleEditWin.isEditing = true;
		});
		tf.addEventListener('blur', function(e) {
			// si la nouvelle valeur n'est pas valide
			if (tf.value.match(/[^0-9]+/)) {
				tf.focus();
			} else {
				TailleEditWin.isEditing = false;
			}
		});
	}
	else if (type == 'picker') {
		var cancel = Ti.UI.createButton({
			title:'Annuler'
		});
		cancel.addEventListener('click', function(e) {
			popover.hide({animated:true});
		});
		
		var validate = Ti.UI.createButton({
			style:Ti.UI.iPhone.SystemButtonStyle.DONE,
			title:'Valider'
		});
		validate.addEventListener('click', function(e) {
			popover.hide({animated:true});
			tf.value = picker.getSelectedRow(0).title;
			updateNetPrice();
		});
		
		var popover = Ti.UI.iPad.createPopover({ 
			arrowDirection:Ti.UI.iPad.POPOVER_ARROW_DIRECTION_TOP,
			leftNavButton:cancel,
			rightNavButton:validate,
			width:200,
			height:200
		});
		
		var picker = Ti.UI.createPicker({
			selectionIndicator:true
		});
		var pickerData = getRemisePickerData();
		picker.data = pickerData;
		picker.add(pickerData);
		popover.add(picker);
		
		tf.addEventListener('singletap', function(e) {
			if (!TailleEditWin.isEditing) {
				picker.setSelectedRow(0, findValue(picker.data, tf.value), false);
				popover.show({
					view:tf,
					animated:true
				});
			}
		});
	}
	
	return tf;
}

function createLabelField(data) {
	var lbl = Ti.UI.createLabel({
		text:data,
		left:styles.editLeft,
		width:'auto',
		height:'auto'
	});
	
	return lbl;
}

function createField(name, data, type) {
	// on veut que des strings
	data = '' + data;
	
	var view = Ti.UI.createView({
		top:styles.vGap,
		left:styles.editLeft,
		right:styles.editRight,
		height:'auto',
		backgroundColor:styles.editBackgroundColor,
		borderColor:styles.editBorderColor,
		borderWidth:1,
		borderRadius:15
	});
	
	var title = Ti.UI.createLabel({
		text:name,
		textAlign:'right',
		left:styles.editLeft,
		width:'auto'
	});
	view.add(title);
	TailleEditWin.titles.push(title);
	
	var line = Ti.UI.createView({
		backgroundColor:styles.editBorderColor,
		width:1
	});
	view.add(line);
	TailleEditWin.lines.push(line);
	
	var field;
	if (type == 'picker') {
		field = createEditableField(data, type);
	} else if (type == 'lbl') {
		field = createLabelField(data);
	} else if (type == 'tf') {
		field = createEditableField(data, type);
	} else {
		Ti.API.error('type non géré');
	}
	field.color = styles.editFieldColor;
	TailleEditWin.fields[name] = field;
	
	var fieldview = Ti.UI.createView({
		height:'auto'
	});
	fieldview.add(field);
	TailleEditWin.fieldViews.push(fieldview);
	view.add(fieldview);
	
	TailleEditWin.scrollView.add(view);
}

function pageSetting() {
	var maxWidth = 0, maxHeight = 0, title, line, field, fieldView;
	for (var i in TailleEditWin.titles) {
		if (i >= 0) {
			title = TailleEditWin.titles[i];
			if (title.width > maxWidth) {
				maxWidth = title.width;
			}
		}
	}
	
	for (i in TailleEditWin.fields) {
		if (i) {
			field = TailleEditWin.fields[i];
			if (field.height > maxHeight) {
				maxHeight = field.height;
			}
		}
	}
	
	for (i in TailleEditWin.titles) {
		if (i >= 0) {
			title = TailleEditWin.titles[i];
			line = TailleEditWin.lines[i];
			fieldView = TailleEditWin.fieldViews[i];
			
			title.width = maxWidth;
			title.height = line.height = maxHeight;
			line.left = right(title) + styles.editLeft;
			fieldView.left = right(line);
		}
	}
	
	for (i in TailleEditWin.fields) {
		if (i) {
			field = TailleEditWin.fields[i];
			field.height = maxHeight;
		}
	}
}


TailleEditWin.open = function(data) {
	TailleEditWin.edited = false;
	TailleEditWin.data = data;
	
	TailleEditWin.win = createWindow({
		title:'Edition commande'
	});
	
	TailleEditWin.scrollView = Titanium.UI.createScrollableView({
		layout:'vertical',
		contentWidth:'auto',
		contentHeight:'auto',
		top:0,
		showVerticalScrollIndicator:true
	});
	TailleEditWin.win.add(TailleEditWin.scrollView);
	
	var flexSpace = Titanium.UI.createButton({
		systemButton:Titanium.UI.iPhone.SystemButton.FLEXIBLE_SPACE
	});
	
	var btSaveOrder = Ti.UI.createButton({
		style:Ti.UI.iPhone.SystemButtonStyle.DONE,
		title:'Finaliser commande'
	});
	btSaveOrder.addEventListener('click', function(e) {
		OrderWin.open(data.orderNumber, TailleEditWin.toolbar);
	});

	var btSaveSize = Ti.UI.createButton({
		style:Ti.UI.iPhone.SystemButtonStyle.DONE,
		title:'Valider'
	});
	btSaveSize.addEventListener('click', function(e) {
		TailleEditWin.edited = true;
		TailleEditWin.data.orderedQty = TailleEditWin.fields['Quantité'].value;
		TailleEditWin.data.discount = TailleEditWin.fields['Remise (%)'].value;
		TailleEditWin.data.netPrice = TailleEditWin.fields['Prix net'].text;
		SplitView.detailNav.close(TailleEditWin.win);
	});
	
	TailleEditWin.toolbar = Titanium.UI.createToolbar({
		items:[btSaveOrder, flexSpace, btSaveSize],
		bottom:0,
		borderTop:true,
		borderBottom:false,
		translucent:true
	});
	TailleEditWin.win.add(TailleEditWin.toolbar);
	
	createField('Taille', data.size, 'lbl');
	createField('Quantité', data.orderedQty, 'tf');
	createField('Quantité annulée', data.canceledQty, 'lbl');
	createField('Prix de vente', data.price, 'lbl');
	createField('Remise (%)', data.discount, 'picker');
	createField('Prix net', data.netPrice, 'lbl');
	pageSetting();
	
	
	/*-------------------------------------*/
	if (test == 1) {
		SplitView.detailNav.open(TailleEditWin.win);
	} else {
		return TailleEditWin.win;
	}
};